package com.ruoyi.im.controller;

import com.ruoyi.im.entity.*;
import com.ruoyi.im.service.ImService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class IMController {

    @Autowired
    private ImService imService;

    @PostMapping("/addGroup")
    public AjaxResult createGroup(@RequestBody GroupEntity entity) {
        return AjaxResult.success(imService.createGroup(entity));
    }

    @PostMapping("/addFriend")
    public AjaxResult addFriend(@RequestBody AddFriendEntity entity) {
        Integer userId = entity.getUserId();
        Integer receiverId = entity.getReceiverId();
        return AjaxResult.success(imService.addFriend(userId, receiverId));
    }

    @PostMapping("/queryFriend")
    public AjaxResult queryFriend(@RequestBody UserEntity entity) {
        Integer userId = entity.getUserId();
        return AjaxResult.success(imService.queryFriend(userId));
    }


    /**
     * 查询用户的群组
     *
     * @param entity
     * @return
     */
    @PostMapping("/queryGroup")
    public AjaxResult queryGroup(@RequestBody UserEntity entity) {
        Integer userId = entity.getUserId();
        return AjaxResult.success(imService.getUserGroup(userId));
    }

    /**
     * 查询指定群组的用户信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/groupUsers")
    public AjaxResult groupUsers(@RequestBody GroupEntity entity) {
        Integer groupId = entity.getGroupId();
        return AjaxResult.success(imService.getGroupUserList(groupId));
    }


    /**
     * 注册一个用户
     *
     * @param entity
     * @return
     */
    @PostMapping("/registerUser")
    public AjaxResult registerUser(@RequestBody UserEntity entity) {
        return AjaxResult.success(imService.registerUser(entity));
    }

    /**
     * 删除群组信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/delGroup")
    public AjaxResult delGroup(@RequestBody GroupEntity entity) {
        imService.delGroup(entity.getGroupId(), entity.getCreateUserId());
        return AjaxResult.success();
    }



    /**
     * 查询用户的基本信息
     *
     * @param entity
     * @return
     */
    @PostMapping("/userInfo")
    public AjaxResult userInfo(@RequestBody UserEntity entity) {
        Integer userId = entity.getUserId();
        return AjaxResult.success(imService.userInfo(userId));
    }

    /**
     * 生成消息id
     * @param entity
     * @return
     */
    @PostMapping("/geneMsgId")
    public AjaxResult geneMsgId(@RequestBody MsgIdEntity entity) {
        return AjaxResult.success(imService.geneMsgId(entity));
    }


}
